/*----------------------------------------------------------------------
 * threadtest.cc 
 *Simple test case for the threads assignment.
 *
 *	Create two threads, and have them context switch
 *	back and forth between themselves by calling Thread::Yield, 
 *	to illustratethe inner workings of the thread system.
 *	
 * This is modified to test multithread in userprogram
 * By OceanMaster
 *---------------------------------------------------------------------*/

#include "syscall.h"

/*----------------------------------------------------------------------
 * SimpleThread
 * 	Loop 5 times, yielding the CPU to another ready thread 
 *	each iteration.
 *
 *	"which" is simply a number identifying the thread, for debugging
 *	purposes.
 *---------------------------------------------------------------------*/

int which;
int lock;

void
SimpleThread()
{
    int i;
    int number[2];

	AcquireLock(lock);
	number[0] = which++;
	ReleaseLock(lock);

    for (i = 0; i < 5; i++) 
	{
		number[1] = i;
		WriteFormat("*** thread %d looped %d times\n", 31, number, 2);
        Yield();
    }
	Exit(0);
}

/*----------------------------------------------------------------------
 * ThreadTest Main
 * 	Set up a ping-pong between two threads, by forking a thread 
 *	to call SimpleThread, and then calling SimpleThread ourselves.
 *---------------------------------------------------------------------*/

int
main()
{
    WriteFormat("Entering SimpleTest\n",21,0,0);
	
	lock = CreateLock("ID_Lock",7);

	Fork(SimpleThread);
    SimpleThread();
	Exit(0);
}

